#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <gmp.h>


int main(void){
    /*declare and set the initial values of the variables*/
    unsigned long int i, num_pontos = 0;
    unsigned long int num_iteracoes = 99493984;

    double x, y;
    mpf_t PI;
    mpf_t cont_points;
    mpf_t total;

    mpf_set_default_prec(40000016);
    mpf_init_set_ui(PI, 0L);
    mpf_init_set_ui(cont_points, 0L);
    mpf_init_set_ui(total, 0L);

    /*In each iteration, 2 random numbers between 0 and 1 will be checked. If the point is inside the circle
      we increment num_pontos*/
    for(i = 0; i < num_iteracoes; i++) {
        x = drand48();
        y = drand48();
        if( pow((x*x + y*y),0.5) <= 1) num_pontos++;
    }

    /*After the iterations, we are able to calc the value of PI*/
    mpf_set_si(cont_points, num_pontos);
    mpf_set_si(total, num_iteracoes);

    mpf_mul_ui(PI, cont_points, 4L);
    mpf_div(PI, PI, total);

    /*We print the value on the screen*/
    mpf_out_str(stdout, 10, 40000016, PI);

    mpf_clear(cont_points);
    mpf_clear(total);
    mpf_clear(PI);

    return 0;
}





